from apps.common.base.dao import BaseDao
from apps.system.role.serializers import *
from apps.common.extension.customException import VerifyException

class RoleDao(BaseDao):
    MODEL_CLASS = Role
    SERI_CLASS = RoleBaseSerializer

    def create(self, data):
        if data.get('key'):
            roles = RoleDao().findAllByLesseeId(data.get('lessee')).filter(key=data.get('key'))
            if roles.exists():
                raise VerifyException('角色关键字已存在')
        return super().create(data)

    def update(self, obj_id, data):
        if data.get('key'):
            obj = RoleDao().findOneByObjId(obj_id)
            if obj.key != data.get('key'):
                roles = RoleDao().findAllByLesseeId(data.get('lessee')).filter(key=data.get('key'))
                if roles.exists():
                    raise VerifyException('角色关键字已存在')
        return super().update(obj_id, data)